package rmi;

import org.apache.log4j.Logger;

import rmi.PolicyFileLocator;
import testnode.TestNode;

/**
 * class to do some common things for client & server to get RMI working
 *
 * @author srasul
 *
 */
public abstract class RmiStarter {
	
	Logger logger = Logger.getLogger(this.getClass());
     
	public RmiStarter() {

    	try {
			StringBuilder codebase = new StringBuilder();
			
			String projectPath = System.getProperty("user.dir");
						
			codebase.append(TestNode.class.getProtectionDomain().getCodeSource().getLocation().toString())//.append("file:/").append(projectPath).append("/bin/")
					.append(" file:/").append(projectPath).append("/bin/lib/rmiio-2.0.0.jar")
					.append(" file:/").append(projectPath).append("/bin/lib/log4j-1.2.9.jar")
					.append(" file:/").append(projectPath).append("/bin/lib/commons-logging.jar")
					.append(" file:/").append(projectPath).append("/bin/lib/commons-logging-api.jar");
			
			
			/*String projectPath = "http://localhost/rmilib";
			codebase.append(projectPath).append("/gmix.jar ")
					.append(projectPath).append("/bin/lib/rmiio-2.0.0.jar ")
					.append(projectPath).append("/bin/lib/log4j-1.2.9.jar ")
					.append(projectPath).append("/bin/lib/commons-logging.jar ")
					.append(projectPath).append("/bin/lib/commons-logging-api.jar");*/
			System.setProperty("java.rmi.server.codebase", codebase.toString());

			
			System.setProperty("java.security.policy", PolicyFileLocator.getLocationOfPolicyFile());

			if(System.getSecurityManager() == null) {
			    System.setSecurityManager(new SecurityManager());
			}

			doCustomRmiHandling();
		} catch (NullPointerException e) {
			logger.error("Error in RMIStarter!", e);
		}
    }

    /**
     * extend this class and do RMI handling here
     */
    public abstract void doCustomRmiHandling();

}
